package com.ham.mud;

import com.ham.mud.configuration.FilePaths;
import com.ham.mud.configuration.NumericConstants;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * Created by hlucas on Jun 16, 2011 at 4:43:14 PM
 */
public class Colors {
    public static final String NEW_LINE = System.getProperty("line.separator");
    public static final char ESCAPE_CHAR = (char) 27;

    public static final String RESET =              ESCAPE_CHAR + "[0m";
    public static final String BOLD_ON =            ESCAPE_CHAR + "[1m";
    public static final String ITALICS_ON =         ESCAPE_CHAR + "[3m";
    public static final String UNDERLINE_ON =       ESCAPE_CHAR + "[4m";
    public static final String INVERSE_ON =         ESCAPE_CHAR + "[7m";
    public static final String STRIKETHROUGH_ON =   ESCAPE_CHAR + "[9m";
    public static final String BOLD_OFF =           ESCAPE_CHAR + "[22m";
    public static final String ITALICS_OFF =        ESCAPE_CHAR + "[23m";
    public static final String UNDERLINE_OFF =      ESCAPE_CHAR + "[24m";
    public static final String INVERSE_OFF =        ESCAPE_CHAR + "[27m";
    public static final String STRIKETHROUGH_OFF =  ESCAPE_CHAR + "[29m";
    public static final String BLACK =              ESCAPE_CHAR + "[30m";
    public static final String RED =                ESCAPE_CHAR + "[31m";
    public static final String GREEN =              ESCAPE_CHAR + "[32m";
    public static final String YELLOW =             ESCAPE_CHAR + "[33m";
    public static final String BLUE =               ESCAPE_CHAR + "[34m";
    public static final String MAGENTA =            ESCAPE_CHAR + "[35m";
    public static final String CYAN =               ESCAPE_CHAR + "[36m";
    public static final String WHITE =              ESCAPE_CHAR + "[37m";
    public static final String DEFAULT =            ESCAPE_CHAR + "[39m";

    public static String format(String s, boolean withColor) {
        char[] chars = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        int at = 0;
        for(int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if(c == '{') {
                i++;
                c = chars[i];
                if(c != '{') {
                    if(withColor) {
                        if(Character.isUpperCase(c) || c == 'd') {
                            sb.append(Colors.BOLD_ON);
                        } else if(c != 'x') {
                            sb.append(Colors.BOLD_OFF);
                        }

                        c = Character.toLowerCase(c);
                        switch(c) {
                            case 'r':
                                sb.append(Colors.RED);
                                break;
                            case 'g':
                                sb.append(Colors.GREEN);
                                break;
                            case 'y':
                                sb.append(Colors.YELLOW);
                                break;
                            case 'b':
                                sb.append(Colors.BLUE);
                                break;
                            case 'm':
                                sb.append(Colors.MAGENTA);
                                break;
                            case 'c':
                                sb.append(Colors.CYAN);
                                break;
                            case 'w':
                                sb.append(Colors.WHITE);
                                break;
                            case 'd':
                                sb.append(Colors.BLACK);
                                break;
                            case 'x':
                                sb.append(Colors.RESET);
                                break;
                            default:
                                sb.append(Colors.RESET);
                                break;
                        }
                    }
                    i++;
                    c = chars[i];
                }
            }
            sb.append(c);
            at++;
            if(at >= NumericConstants.SCREEN_WIDTH && c == ' ') {
                sb.append(NEW_LINE);
                at = 0;
            }
        }

        return sb.toString();
    }

    public static String getValueAndSpaces(int number, String name) {
        return getValueAndSpaces(number, name, false);
    }

    public static String getValueAndSpaces(int number, String name, boolean rightAlign) {
        if(rightAlign) return getSpaces(number, name) + name;
        return name + getSpaces(number, name);
    }

    public static String getSpaces(int number, String name) {
        int spaces = number - name.length();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < spaces; i++) {
            sb.append(" ");
        }
        return sb.toString();
    }

    public static void printASCII(String name, ServerConnection connection) {
        if(!connection.getPlayer().isAsciiEnabled()) return;

        File file = new File(FilePaths.getAsciiFilePath(name));
        try {
            FileInputStream inputStream = new FileInputStream(file);
            DataInputStream in = new DataInputStream(inputStream);
            String nextString = "";
            while((nextString = in.readLine()) != null) {
                connection.println(nextString);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void printASCII(String zone, String name, ServerConnection connection) {
        if(!connection.getPlayer().isAsciiEnabled()) return;

        File file = new File(FilePaths.getAsciiFilePath(zone, name));
        try {
            FileInputStream inputStream = new FileInputStream(file);
            DataInputStream in = new DataInputStream(inputStream);
            String nextString = "";
            while((nextString = in.readLine()) != null) {
                connection.println(nextString);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
